function PullDownTree() {
	this.tree = null;
	this.treeDiv=null;
	this.inputid=null;
	this.valueid=null;
	this.ischeck=true;
	this.clickCheck = null;
	this.chooseids=new Array();
	this.choosename=new Array();
	this.ztreeData = [];
	this.setting = {};
	var th = this;
	
	/**
	 * url 请求方法
	 * id 点击元素
	 * param 参数
	 * vid  赋值id
	 * ischeck 是否显示复选框
	 * setValue 赋值函数
	 * dataPreHandle 数据提前处理
	 * clickCheck 点击选中函数
	 */
	this.init=function(url,id,param,vid,ischeck, setValue, dataPreHandle, clickCheck, dataOptions, treeId){
		this.treeId = treeId || "ztree-pull-1";
		this.initTreeDiv(id);
		this.inputid = id;
		this.valueid = vid;
		this.setValue = setValue;
		this.clickCheck = clickCheck;
		$.getJSON(url,param,function(data){
			if(data.status==200){
				th.setting = {
					check: { /**复选框**/
						enable: ischeck,
						chkboxType: {"Y":"", "N":""},//上下级互相不影响
					},
					view : {
						expandSpeed : 100
					},
					data : $.extend({
						simpleData : {
							enable : true,
							idKey : "id",
							pIdKey : "pid",
							rootPId : 0
						}
					}, dataOptions),
					callback : {
						beforeClick : th.beforeClick,
						onClick: th.zTreeOnClick,
						onCheck: th.zTreeOnCheck
					}
				};
				var data = data.data;
				data = th.initDataValue(data);//初始化选中
				if(dataPreHandle instanceof Function){
					try {
						data = dataPreHandle(data);
						data = dataPreHandle(data,vid);
					} catch (e) {
						console.error(e);
					}
					
				}
				th.ztreeData = data;
				this.tree = $.fn.zTree.init($("#" + th.treeId),th.setting,data);
			}else{
				sys.msg(data);
			}
		})
		
	};
	
	
	this.filterZtree = function(that){
		var _map = [];
		var value = $(that).val();
		if(!th.ztreeData || th.ztreeData.length == 0){
			return ;
		}
		for(var key in th.ztreeData){
			if((th.ztreeData[key].name+"").indexOf(value+"") > -1){
				_map.push(th.ztreeData[key]);
			}
		}
		this.tree = $.fn.zTree.init($("#"+th.treeId),th.setting,_map);
	}
	
	this.initDataValue = function(data){
		var ids = $(th.valueid).val();
		if(!ids) return data;
		var idss = ids.split(",");
		for(var key in data){
			for(var index in idss){
				if(data[key].id == idss[index]){
					data[key].checked = true;
				}
			}
		}
		return data;
	}
	
	this.initTreeDiv = function(id){
		
		$("#ztree-container").remove();
		var that = this.thisObj;
		var ztree="<div id='"+th.treeId+"'  class='ztree'></div>";
		var container = "<div id='ztree-container' style='padding:5px;max-height:400px;position:absolute;border:1px solid #ccc;background-color:#fff;z-index:10000;overflow-y: scroll;'>%search_input%%ztree%</div>";
		var search_input = "<input class='ztree-search'  style='width:100%'  />";
		var html = container.replace("%ztree%",ztree).replace("%search_input%",search_input);
		$("body").append(html);
		var pd_top = window.parseInt($(id).css("padding-top").replace("px",""));
		var pd_bottom = window.parseInt($(id).css("padding-bottom").replace("px",""));
		if(pd_top==null){
			pd_top=0
		}
		if(pd_bottom==null){
			pd_bottom=0
		}
		$("#ztree-container").find(".ztree-search").on('input propertychange', function(event){
			th.filterZtree($(this));
		});
		
		$("#ztree-container").css({top:($(id).offset().top+$(id).height()+pd_top+pd_bottom)+"px",left:$(id).offset().left/*,width:$(id).width()+"px"*/});
	};
	this.beforeClick = function(treeId, treeNode) {
		th.chooseids.length=0;
		th.choosename.length=0;
		
		if(th.clickCheck instanceof Function){
			if(!th.clickCheck(treeId, treeNode)){
				return false;
			}
		}
		$(th.inputid).val(treeNode.name);
		$(th.inputid).attr("data-id",treeNode.id);
		if(th.valueid!=null){
			$(th.valueid).val(treeNode.id);
		}
		$("#ztree-container").hide();
		if(th.setValue instanceof Function){
			th.setValue(treeNode.id, $.extend({}, treeNode));
		}
		
		// 选择节点后search
		var existSearchTree = $(".oper-search").find(th.inputid).size() > 0;
		if (existSearchTree) {
		
		}
		
		return false;
	};
	this.zTreeOnClick=function(event, treeId, treeNode) {
		//alert(treeNode.tId + ", " + treeNode.name);
	};
	this.zTreeOnCheck=function(event, treeId, treeNode) {
		//alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
		var treeObj = $.fn.zTree.getZTreeObj(treeId);
		var nodes = treeObj.getCheckedNodes();
		th.chooseids.length=0;
		th.choosename.length=0;
		for(var i=0;i<nodes.length;i++){
			th.chooseids.push(nodes[i].id);
			th.choosename.push(nodes[i].name);
		}
		$(th.inputid).val(th.choosename.length>0?th.choosename.toString():"");
		if(window.__SET_ZTREE_VALUE){
			$(th.valueid).val(th.chooseids? th.chooseids.join(","): "");
		}
	};
};
var PullDownTree_obj = new PullDownTree();

/**捕获界面点击时间隐藏选择框*/
$(document.documentElement).mousedown(function(e){
	if($("#ztree-container").is(":visible")){
		var x = $("#ztree-container").offset().left;
		var y = $("#ztree-container").offset().top;
		var w = $("#ztree-container").outerWidth(true);
		var h = $("#ztree-container").outerHeight(true);
		var ex= e.pageX;
		var ey= e.pageY;
		if(!((ex>x&&(ex<x+w))&&(ey>y&&(ey<y+h)))){
			$("#ztree-container").hide();
		}
	}
});

